package com.jt.redis;

import redis.clients.jedis.Jedis;

import java.util.UUID;

public class SSODemo01 {

    static String doLogin(String username,String password){
        if (!"jack".equals(username))
            throw new IllegalArgumentException("用户不存在");
        if (!"123456".equals(password))
            throw new IllegalArgumentException("密码不正确");
        Jedis jedis = new Jedis("192.168.126.129", 6379);
        String token = UUID.randomUUID().toString();
        jedis.set(token,username);
        jedis.expire(token, 2);
        jedis.close();
        return token;

    }

    static  Object doGetResource(String token){
        if (token==null){
            throw new IllegalArgumentException("请先登录");
         }
        Jedis jedis=new Jedis("192.168.126.129",6379);

        String username = jedis.get(token);
        jedis.close();
        if (username==null)
            throw new RuntimeException("登录超时,请重新登录");
        System.out.println("继续访问资源");
        return "the resource of user";
    }

    public static void main(String[] args) {
        String token=null;
        token = doLogin("jack", "123456");
        doGetResource(token);
    }

}
